package net.jeeshop.core.pay.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import net.jeeshop.core.pay.IPay;
import net.jeeshop.core.pay.alipay.alipayescow.config.AlipayConfig;

/**
 * Created by xvhuichuang on 2017/7/9.
 */
public class ALiPay implements IPay<String, String> {

    @Override
    public String pay(String... t) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.SERVER_URL, AlipayConfig.APP_ID,
                AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE
        );
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL);
        alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL);//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent(t[0]);
        try {
            //得到二维码链接
//            alipayClient.pageExecute(alipayRequest).getSubCode();
            return alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String refund(String... t) {
        /**
         * 阿里退款
         */
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.SERVER_URL, AlipayConfig.APP_ID,
                AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE
        );
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent(t[0]);
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
        }
        if (response != null && response.isSuccess()) {
            return response.getBody();
        }
        return null;
    }
}
